﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Marbles.Core.Authentication;
using Marbles.Core.Content.Sites;
using System.Web.UI;
using Marbles.Core.Filters;
using System.IO;
using System.Web.UI.HtmlControls;
using System.Web;
using Marbles.Core.Handlers;
using Marbles.Core.Content.Pages;
using Marbles.Core.Content.Analytics;
using System.Web.UI.WebControls;
using Marbles.Core.Content.CDN;
using Marbles.Core.Settings;

namespace Marbles.Web.Admin.Downloads
{
    public partial class Download : System.Web.UI.Page
    {

        public Marbles.Core.Authentication.AuthenticationProvider AuthenticationProvider
        {
            get { return Marbles.Core.Authentication.AuthenticationProvider.CreateProviderInstance(); }
        }

        public bool IsEditor
        {
            get
            {
                IToken token = this.AuthenticationProvider.CurrentToken;
                return (token == null) ? false : token.IsEditor;
            }
        }

        public bool IsRoot
        {
            get
            {
                IToken token = this.AuthenticationProvider.CurrentToken;
                return (token == null) ? false : token.IsRoot;
            }
        }

        public bool IsAdministrator
        {
            get
            {
                IToken token = this.AuthenticationProvider.CurrentToken;
                return (token == null) ? false : token.IsAdministrator;
            }
        }

        public bool isViewer
        {
            get
            {
                IToken token = this.AuthenticationProvider.CurrentToken;
                return (token == null) ? true : token.IsViewer;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if ((!IsRoot))
            {
                Response.Clear();
                Response.StatusCode = 404;
                Response.StatusDescription = "File Not Found.";
                Response.Flush();
                Response.End();
                return;
            }

            string path = Request.Params["path"];
            if (string.IsNullOrEmpty(path))
            {
                Response.Clear();
                Response.StatusCode = 404;
                Response.StatusDescription = "File Not Found.";
                Response.Flush();
                Response.End();
                return;
            }

            path = Server.MapPath(path);
            if (!File.Exists(path))
            {
                Response.Clear();
                Response.StatusCode = 404;
                Response.StatusDescription = "File Not Found.";
                Response.Flush();
                Response.End();
                return;
            }

            FileInfo info = new FileInfo(path);
            Response.Clear();
            Response.ContentType = Marbles.Core.Content.ContentTypes.GetMimeType(info.Extension);
            Response.AddHeader("content-disposition", string.Format("attachment;filename={0}", info.Name));
            FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            byte[] buffer = new Byte[10000];
            int length = 1;
            long dataToRead = 0;
            
            //dataToRead = iStream.Length;

            while (length > 0)
            {
                if (Response.IsClientConnected)
                {
                    length = fs.Read(buffer, 0, 10000);
                    Response.OutputStream.Write(buffer, 0, length);
                    Response.Flush();
                    buffer = new Byte[10000];
                    dataToRead = dataToRead - length;
                }
                else
                {
                    dataToRead = -1;
                }
            }
            fs.Close();
            Response.Flush();
            Response.End();
        }
    }
}